package project.service.impl;

import project.dao.StudentDao;
import project.dao.impl.StudentDaoImpl;
import project.entity.Student;
import project.service.StudentService;
import project.utils.DruidUtil;
import project.utils.Page;

import java.util.List;

/*
杨威
2021/9/11
17:35
*/
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao=new StudentDaoImpl();
    public List<Student> showAllStudent(){
        List<Student> students = studentDao.studentList();
        DruidUtil.close();
        return students;
    }

    @Override
    public List<Student> showAllStudentByPage(Page page) {
        List<Student> students = studentDao.selectAllByPage(page);
        Long totalCount = studentDao.selectCount();
        page.setTotalCount(totalCount);
        DruidUtil.close();
        return students;
    }

    @Override
    public Long showCount() {
        return null;
    }

    @Override
    public int insertStudent(Student stu) {
        int insert=0;
        try {
            DruidUtil.begin();
            insert = studentDao.insert(stu);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return insert;
    }

    @Override
    public int updateStudent(Student stu) {
        int update=0;
        DruidUtil.begin();
        try {
            update=studentDao.update(stu);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }

        return update;
    }

    @Override
    public int deleteStudent(int stuId) {
        int delete=0;
        DruidUtil.begin();
        try {
            delete=studentDao.delete(stuId);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return delete;
    }

    @Override
    public Student showStudentById(int stuId) {
        Student student = studentDao.selectById(stuId);
        DruidUtil.close();
        return student;
    }
}
